/**
 * 
 */
package com.aspire.wap.core.io;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;

/**
 * <p>文件工具类</p>
 * @author sunquanzhi
 * @version 1.0
 * @deprecated
 */
public class FileUtil {
	/**
	 * <p>创建一个文件</p>
	 * @param output
	 * @param content
	 * @param encode 如果不为空，按照该编码取内容
	 * @throws Exception
	 */
	public static void createFile(String output, String content,String encode) throws Exception {
	    OutputStreamWriter fw = null;
	    PrintWriter out = null;
	    try {
	      if(encode == null)
	      {
	    	  fw = new OutputStreamWriter(new FileOutputStream(
	    	          output));
	      }else{
	    	  fw = new OutputStreamWriter(new FileOutputStream(
	    	          output), encode);
	      }
	      out = new PrintWriter(fw);
	      out.print(content);
	    } catch (Exception ex) {
	      throw new Exception(ex);
	    }finally{
	      if (out != null)
	          out.close();
	      if (fw != null)
	          fw.close();
	    }

	  }
	/**
	 * <p>转移文件</p>
	 * @param input
	 * @param output
	 * @throws Exception
	 */
	public static void move(String input, String output) throws Exception {
	    File inputFile = new File(input);
	    File outputFile = new File(output);
	    try {
	      inputFile.renameTo(outputFile);
	    } catch (Exception ex) {
	      throw new Exception("Can not mv" + input + " to " + output +
	                          ex.getMessage());
	    }
	  }
	 /**
	  * <p>创建目录</p>
	  * @param home
	  * @throws Exception
	  */
	 public static void makeDir(String home) throws Exception {
		    File homedir = new File(home);
		    if (!homedir.exists()) {
		      try {
		        homedir.mkdirs();
		      } catch (Exception ex) {
		        throw new Exception("Can not mkdir :" + home +
		                            " Maybe include special charactor!");
		      }
		    }
		}

	 /**
	  * <p>读取文件</p>
	  * @param input
	  * @param encode 如果不为空，按照该编码取内容
	  * @return 文件内容
	  * @throws Exception
	  */
	 public static String readFile(String input,String encode) throws Exception {
		    char[] buffer = new char[4096];
		    int len = 0;
		    StringBuffer content = new StringBuffer(4096);
		    InputStreamReader fr = null;
		    BufferedReader br = null;
		    try {
		    if(encode != null)
		    {
		      fr = new InputStreamReader(new FileInputStream(input), encode);
		    }else{
		    	 fr = new InputStreamReader(new FileInputStream(input));
		    }
		    br = new BufferedReader(fr);
		      while ( (len = br.read(buffer)) > -1) {
		        content.append(buffer, 0, len);
		      }
		    } catch (Exception e) {
		      throw new Exception(e);
		    }finally{
		      if (br != null)
		         br.close();
		      if (fr != null)
		         fr.close();
		    }
		    return content.toString();
		  }
	 /**
	  * <p>删除文件或目录</p>
	  * @param input
	  */
	 public static boolean remove(String input)
	 {
		 File file = new File(input);
		 if(file.isDirectory())
		 {
			 for(File f : file.listFiles())
			 {
				 remove(f.getAbsolutePath());
			 }
		 }
		 return file.delete();
		
	 }
	 /**
	  * <p>复制文件</p>
	  * @param input 源文件
	  * @param output 目的文件
	  * @throws Exception
	  */
	 public static void copyFile(String input,String output)throws Exception 
	 {
		 File sourceDir = new File(input);
		 if(!sourceDir.exists())
		 {
			 throw new Exception(input +" file is  not exist");
		 }
		 createFile(output,readFile(input,null),null);
		
	 }
	 /**
	  * <p>复制目录</p>
	  * @param input 源目录
	  * @param output 目的目录
	  * @throws Exception
	  */
	 public static void copyDir(String input,String output)throws Exception 
	 {
		 File sourceDir = new File(input);
		 if(!sourceDir.isDirectory())
		 {
			 throw new Exception(input +" is  not a Directory");
		 }
		 makeDir(output);
		 File[] files = sourceDir.listFiles();
		 for(File file : files)
		 {
			 
			 if(file.isDirectory())
			 {
				 copyDir(file.getAbsolutePath(),output+File.separator+file.getName());
			 }else{
				 copyFile(file.getAbsolutePath(),output+File.separator+file.getName());
			 }
		 }
	 }
}
